import axios from "axios";
import {message} from "ant-design-vue";
import {CHANGE_BASE_URL} from '@/constants/localAndProd';

// 区分开发和生产环境
const DEV_BASE_URL = "http://localhost:8123";
const PROD_BASE_URL = "http://60.204.233.140";

// 创建 Axios 实例
const myAxios = axios.create({
  baseURL: "http://" + CHANGE_BASE_URL,
  timeout: 10000,
  withCredentials: true,
});

// 请求拦截器
axios.interceptors.request.use(function (config) {
    // Do something before request is sent
    return config;
}, function (error) {
    // Do something with request error
    return Promise.reject(error);
});

// 响应拦截器
axios.interceptors.response.use(function (response) {
    const {data} = response;
    if(data.code === 40100){
        // 如果不是获取用户信息，且当前不在登录页面时，跳转到登录页面
        if(!response.request.responseURL.includes('user/get/login') && !window.location.pathname.includes('/user/login')) {
            message.warning("请先登录后尝试");
            window.location.href = `/user/login?redirect=${window.location.href}`
        }

    }
    return response;
}, function (error) {

    return Promise.reject(error);
});


export default myAxios;
